package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;

import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name ="展示所有模块数据")
    public String findAll(@RequestParam(name = "page",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "10")int pageSize){
  
        PageInfo<Module> pageInfo = moduleService.findByPage(pageNum, pageSize);

        request.setAttribute("page",pageInfo);
        return "system/module/module-list";
    }

    @RequestMapping(value = "/toAdd",name ="进入模块新增页面")
    public String toAdd(){
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/edit",name ="保存模块方法")
    public String edit(Module module){
        if (StringUtils.isEmpty(module.getId())){
            //id为空说明是新增
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        }
        else {
            //id不为空则说明为修改
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }
    @RequestMapping(value = "/toUpdate",name ="进入模块修改页面")
    public String toUpdate(String id){
        Module module=moduleService.findById(id);
        request.setAttribute("module",module);
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-add";
    }
    @RequestMapping(value = "/delete",name ="删除模块信息")
    public String delete(String id){
       moduleService.deleteById(id);
        return "redirect:/system/module/list.do";
    }

}
